<?php

	class ClassAction extends CommonAction {

			/**
			 * 显示分类页
			 */
			public function index(){
				$db = M('class');
				$arr = $db->where(array('pid'=>0))->select();
				$this->assign('classData',$arr);
				$this->display();
			}

			/**
			 * 显示添加分类
			 */
			public function addClass(){
				 $db = M('class');
				 if(isset($_GET['cid'])){

				 	 if($arr = $db->field('cid,classname')->where(array('cid'=>(int)$_GET['cid']))->find()){
				 	 	 $class = $arr;
				 	   }
				 }
				 if(!is_array($class)){
				 	 	$class = array(
				 	 	 		'pid' =>0,
				 	 	 		'classname'=>'顶级栏目'
				 	 	  );
				 }
				$this->assign('type',$class);

				$classData = $db->select();
				$classData  = fenlei($classData);
				$this->assign('classData',$classData);
				$db = M('type');
				$arr = $db->select();
				if($arr){
					$this->assign('typeData',$arr);
				}

				$this->display();
			}
			/**
			 * 获得子分类
			 */
			public function showClass (){
				$pid = (int)$_POST['pid'];
				$db = M('class');
				$par = $db->where(array('cid'=>$pid))->find();
			    $db = M('class');
			    $arr = $db->where(array('pid'=>$pid))->select();
			    foreach($arr as $k=>$v){
			    	 $arr[$k]['par']=$par['classname'];
			    }
			    echo json_encode($arr);

			}
			/**
			 * 添加分类
			 */
			public function toadd (){
				  $db = M('class');
				  if($db->add($_POST)){
				  	  echo json_encode(array('num'=>1,'mesa'=>'插入成功'));
				  }else{
				  		echo json_encode(array('num'=>0,'mesa'=>'插入失败'));
				  }
			}
	}

?>